using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using UnityEngine.UI;
namespace KFGame
{

    /// <summary>
    /// 后台主页
    /// </summary>
    public class KFBackView : KFBackViewManager
    {
        [System.Serializable]
        public class ChartModule
        {
            [SerializeField]
            private string m_Name;

            [SerializeField]
            private string m_Title;

            [SerializeField]
            private bool m_Selected;

            [SerializeField]
            public GameObject source;

            public string name
            {
                get
                {
                    return m_Name;
                }
                set
                {
                    m_Name = value;
                }
            }

            public string title
            {
                get
                {
                    return m_Title;
                }
                set
                {
                    m_Title = value;
                }
            }

            public bool select
            {
                get
                {
                    return m_Selected;
                }
                set
                {
                    m_Selected = value;
                }
            }

            public Button button { get; set; }
        }

        //选择的行
        int selectRow = 0;

        //总项数量
       // int itemsCount = 2;


        [SerializeField]
        public List<ChartModule> menus = new List<ChartModule>();

        public GameObject menuTemplate;

        public Transform menuPlanel;

        public delegate void MenuEvent(string eventname);
        public MenuEvent onMenuEvent;




        void Start()
        {
            initView();

            initMenus();
        }
        void initMenus()
        {
            foreach (var module in menus)
            {
                var btnName = "btn_" + module.name;
                GameObject btn;
                if (menuPlanel.Find(btnName))
                {
                    btn = menuPlanel.Find(btnName).gameObject;
                    btn.name = btnName;
                }
                else
                {
                    btn = GameObject.Instantiate(menuTemplate);
                    btn.SetActive(true);
                    btn.name = btnName;
                    btn.transform.SetParent(menuPlanel);
                    btn.transform.localPosition = Vector3.zero;
                }
                btn.transform.localScale = Vector3.one;
                module.button = btn.GetComponent<Button>();
                module.button.GetComponentInChildren<Text>().text = module.title;
                module.button.onClick.AddListener(delegate ()
                {
                    selecMenu(module);
                });

            }

            menus[selectRow].button.Select();

        }




        /// <summary>
        /// 由框加推送的事件
        /// </summary>
        /// <param name="e"></param>
        public override void OnEvent(KFEvent e)
        {

            //向上
            if (e.name == KFEEVENTS.KFE_EVENT_TO_UP)
            {
                selectRow--;
                if (selectRow < 0)
                    selectRow = menus.Count - 1;

                menus[selectRow].button.Select();

                return;
            }
            //向下
            if (e.name == KFEEVENTS.KFE_EVENT_TO_DOWN)
            {

                selectRow++;
                if (selectRow >= menus.Count)
                    selectRow = 0;

                menus[selectRow].button.Select();
                return;
            }

            if (e.name == KFEEVENTS.KFE_EVENT_TO_OK)
            {
                selecMeneu(selectRow);
                return;
            }


            //减小
            if (e.name == KFEEVENTS.KFE_EVENT_TO_LEFT)
            {
                selecMeneu(selectRow);
                return;
            }
            //增加
            if (e.name == KFEEVENTS.KFE_EVENT_TO_RIGHT)
            {
                selecMeneu(selectRow);
                return;
            }

            base.OnEvent(e);
        }

        public void createMenu(ChartModule module)
        {

            var menu = GameObject.Instantiate(menuTemplate, menuPlanel.transform);
        }


        /// <summary>
        /// 选择菜单
        /// </summary>
        /// <param name="i"></param>
        public void selecMenu(ChartModule menu)
        {
            Debug.Log("select " + menu.name);
            if (menu.name == "exit")
            {
                close();
                return;
            }
            //以事件开始
            if (menu.name.Contains("event_"))
            {

                if (onMenuEvent != null)
                    onMenuEvent(menu.name);

                if (onViewEvent != null)
                    onViewEvent(menu.name, false);

                return;
            }

            //没有资源 定义不事件
            if (menu.source == null)
            {
                if (onMenuEvent != null)
                    onMenuEvent(menu.name);

                if (onViewEvent != null)
                    onViewEvent(menu.name, false);
                return;
            }

            loadResouce(menu.source);
            Destroy(gameObject);
        }

        /// <summary>
        /// 选择菜单
        /// </summary>
        /// <param name="i"></param>
        public void selecMeneu(int i)
        {
            selecMenu(menus[i]);
        }


    }
}
